<?php namespace sacvoyage\app;



require_once 'sacvoyage/domain/Resource.php';
use \sacvoyage\domain\Resource;

use \sacvoyage\domain\Synchronizer;
require_once 'sacvoyage/util.php';
require_once 'sacvoyage/http/util.php';
use \sacvoyage\http\save_json;
use \sacvoyage\http\parse_path;

use \sacvoyage\Object;






class SyncApplication extends BuiltinApplication {
	
	public function __construct(Resource $resource) {
		$this->_resource=$resource;
	}
	
	public function serve($path) {
		list($path, $rest)=parse_path($path);
		$sys_sync=$this->_resource->getCollection('sys.sync');
		if($path) {
			$config=$sys_sync->read($path);
			if($config) {
				$configs=array($config);
			}
		} else {
			$configs=$sys_sync->find();
		}
		
		foreach($configs as $config) {
			$remote=new \sacvoyage\proxy\Resource(
				$config->address,
				isset($config->password) ? $config->password : null
			);
			
			echo "<h1>Synchronizing with {$config->address}</h2>\n\n";
			$syncronizer=new Synchronizer($this->_resource, $remote, $config);
			$syncronizer->synchronize();
		}
	}
	
	private $_resource;
	
}


?>
